//引入字体图标样式
require("../../assets/fonts/iconfont.css");
//移入页面的css样式
require("./register.less");
//引入axios
const axios = require("axios");
//引入验证码
let CaptchaMini = require("captcha-mini");
//引入自定义js
let obj = require("../../assets/js/my.js");
let $ = obj.$;
document.addEventListener("DOMContentLoaded", function () {
	/*传值,参数配置值，选择性配置*/
	let captcha2 = new CaptchaMini({
		lineWidth: 1, //线条宽度
		lineNum: 2, //线条数量
		dotR: 0, //点的半径
		dotNum: 25, //点的数量
		preGroundColor: [10, 80], //前景色区间
		backGroundColor: [150, 250], //背景色区间
		fontSize: 30, //字体大小
		fontFamily: ["Georgia", "微软雅黑", "Helvetica", "Arial"], //字体类型
		fontStyle: "stroke", //字体绘制方法，有fill和stroke
		content: "abcdefghijklmnopqrstuvwxyz0123456789", //验证码内容
		length: 4, //验证码长度
	});
	let security = "";
	captcha2.draw(document.querySelector("#captcha1"), (r) => {
		security = r.toLowerCase();
	});
	//账号注册跳转
	$("header span").addEventListener("click", function () {
		location.href = "./login.html";
	});

	$("#btn").addEventListener("click", function () {
		//账号验证
		let reg =
			/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|17[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
		if (reg.test(tell.value)) {
			$("#error").textContent = "";
			$(".icon-tishicuowu").style.display = "none";
		} else {
			$("#error").textContent = "手机号格式错误";
			$(".icon-tishicuowu").style.display = "inline";
			return;
		}
		//验证码
		if ($("#security").value.toLowerCase() !== security) {
			$(".icon-tishicuowu").style.display = "inline";
			$("#error").textContent = "验证码输入错误";
			return;
		} else {
			$("#error").textContent = "";
			$(".icon-tishicuowu").style.display = "none";
		}
		//密码
		let reg1 = /^\w{6,18}$/;
		if (reg1.test(psw.value)) {
			$("#error").textContent = "";
			$(".icon-tishicuowu").style.display = "none";
		} else {
			$("#error").textContent = "请输入6-18位(字母、数字、下划线)";
			$(".icon-tishicuowu").style.display = "inline";

			return;
		}
		if ($("#psw1").value !== $("#psw").value) {
			$("#error").textContent = "两次密码不一致";
			$(".icon-tishicuowu").style.display = "inline";

			return;
		} else {
			$("#error").textContent = "";
			$(".icon-tishicuowu").style.display = "none";
		}
		axios
			.post("http://fitness.h5.itsource.cn:3701/api/user/register", {
				account: $("#tell").value,
				password: $("#psw").value,
			})
			.then((res) => {
				if (res.data.errno !== 0) {
					$("#error").textContent = res.data.message;
					return;
				} else {
					location.href = "./login.html";
					alert("恭喜你注册成功");
				}
			});
	});
});
